package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2023/5/4 - 05 - 04 - 18:59
 * @Description: com.sky.service.impl
 * @version: 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishtMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增购物车
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result add(ShoppingCartDTO dto) {
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //判断购物车是否存在商品，存在增加数量
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        ShoppingCart queryShoppingCar = shoppingCartMapper.queryShoppingCar(shoppingCart);

        if (queryShoppingCar != null) {
            queryShoppingCar.setNumber(queryShoppingCar.getNumber()+1);
            shoppingCartMapper.updateNumberById(queryShoppingCar);
        }else {
            //否则新增商品
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //菜品
            if (shoppingCart.getDishId() != null) {
                Dish dish = dishtMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //套餐

            if (shoppingCart.getSetmealId() != null) {
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCartMapper.add(shoppingCart);
        }
        return Result.success();
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public Result getCar() {
        Long currentId = BaseContext.getCurrentId();

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getByUserId(currentId);

        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    @Transactional
    public Result deleteCar() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteCar(currentId);
        return Result.success();
    }

    /**
     * 减少一个商品
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result subCar(ShoppingCartDTO dto) {
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
//        如果商品数量大于1，则修改商品数量-1
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        ShoppingCart queryShoppingCar = shoppingCartMapper.queryShoppingCar(shoppingCart);
        if (queryShoppingCar.getNumber()>1) {
            queryShoppingCar.setNumber(queryShoppingCar.getNumber()-1);
            shoppingCartMapper.updateNumberById(queryShoppingCar);
        }else {
            //        如果商品数量等于1，则删除该商品
            shoppingCartMapper.deleteCarByid(shoppingCart);
        }


        return Result.success();
    }
}
